let trainDao = require("../dao/trainDao.js");
let resultUtils = require("../utils/resultUtils.js");

//所有教练
async function getAlltrains() {
  let result = null;
  try {
    let data = await trainDao.getAlltrains();
    result = resultUtils(data);
  } catch (e) {
    console.log(e);
  }

  return result;
}

//分页
async function list(filter, page = 1, limit = 10) {
  let result;
  try {
    let data = await trainDao.list(filter, page, limit);
    let count = await trainDao.count(filter);
    result = resultUtils(data, count);
  } catch (e) {
    console.log(e);
    result = resultUtils(null, 0, -1, "出错啦");
  }
  return result;
}

//删除
async function deleteById(id) {
  let result;
  try {
    let data = await trainDao.deleteById(id);
    result = resultUtils(data, 0, 0, "删除成功");
  } catch (e) {
    console.log(null, 0, -1, "删除失败");
  }
  return result;
}

//新增
async function add(train) {
    let result;
    try {
        let data = await trainDao.add(train);
        result=resultUtils(data, 0, 0, "删除成功")
    } catch (e) {
        console.log(null, 0, -1, "删除失败");
    }
    return result;
    
}

module.exports.getAlltrains = getAlltrains;
